package com.demo.aop.pointcut;

import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParser;

import java.lang.reflect.Method;

public class AspectJPointcut implements Pointcut{

    private String expression;

    private PointcutParser pointcutParser = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();

    private PointcutExpression pointcutExpression;

    public AspectJPointcut(String expression){
        this.expression = expression;
        this.pointcutExpression = pointcutParser.parsePointcutExpression(expression);
    }

    @Override
    public boolean matchClass(Class clazz) {
        return pointcutExpression.couldMatchJoinPointsInType(clazz);
    }

    @Override
    public boolean matchMethod(Class clazz, Method method) {
        return pointcutExpression.matchesMethodExecution(method).alwaysMatches();
    }

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }
}
